﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using YasharEl.Infrastructure.PresentationModel.Prism;
using System.Collections.ObjectModel;
using System.Diagnostics.Contracts;

namespace RemoteTranslator.AssistentApplication.Modules.VideoTransmit
{
    public class VideoTransmitViewModel : ViewModelBase
    {
        //readonly ObservableCollection<ConnectedUser> connectedUsers = new ObservableCollection<ConnectedUser>();
        //readonly object connectedUsersLock = new object();

        //public ObservableCollection<ConnectedUser> ConnectedUsers
        //{
        //    get
        //    {
        //        return connectedUsers;
        //    }
        //}

        public VideoTransmitViewModel(IVideoTransmitView videoTransmitView)
        {
            Contract.Requires<ArgumentNullException>(videoTransmitView != null, "videoTransmitView could not be null.");
            Title = "Connected Users";
            videoTransmitView.ViewLoaded += delegate { StartMonitoring(); };
        }

        void StartMonitoring()
        {
            //var eventAggregator = Dependency.Resolve<IEventAggregator>();
            //var usersChangedEvent = eventAggregator.GetEvent<UserJoinedEvent>();
            //usersChangedEvent.Subscribe(OnUserJoined);
            //var userDepartedEvent = eventAggregator.GetEvent<UserDepartedEvent>();
            //userDepartedEvent.Subscribe(OnUserDeparted);
            //var usersUpdateEvent = eventAggregator.GetEvent<UsersUpdateEvent>();
            //usersUpdateEvent.Subscribe(OnUsersUpdate);

            //var updateRequestedEvent = eventAggregator.GetEvent<UsersUpdateRequestedEvent>();
            //updateRequestedEvent.Publish(this);
        }

        //void OnUsersUpdate(IEnumerable<ConnectedUser> connectedUsers)
        //{
        //    if (connectedUsers == null)
        //    {
        //        Log.Warn("Attempt to udpate connected users with null argument. Suppressing as this may be on child thread.");
        //        return;
        //    }
        //    var dispatcher = Dependency.Resolve<Dispatcher>();
        //    dispatcher.InvokeIfRequired(() =>
        //    {
        //        lock (connectedUsersLock)
        //        {
        //            this.connectedUsers.Clear();
        //            foreach (var user in connectedUsers)
        //            {
        //                this.connectedUsers.Add(user);
        //            }
        //        }
        //    });
        //}

        //void OnUserDeparted(ConnectedUser connectedUser)
        //{
        //    if (connectedUser == null)
        //    {
        //        Log.Warn("Attempt to remove a connected user with null argument. Suppressing as this may be on child thread.");
        //        return;
        //    }
        //    var dispatcher = Dependency.Resolve<Dispatcher>();
        //    dispatcher.InvokeIfRequired(() =>
        //    {
        //        lock (connectedUsersLock)
        //        {
        //            connectedUsers.Remove(connectedUser);
        //        }
        //    });
        //}

        //void OnUserJoined(ConnectedUser connectedUser)
        //{
        //    if (connectedUser == null)
        //    {
        //        Log.Warn("Attempt to add a connected user with null argument.");
        //        return;
        //    }
        //    var dispatcher = Dependency.Resolve<Dispatcher>();
        //    dispatcher.InvokeIfRequired(() =>
        //    {
        //        lock (connectedUsersLock)
        //        {
        //            if (!connectedUsers.Contains(connectedUser))
        //            {
        //                connectedUsers.Add(connectedUser);
        //            }
        //        }
        //    });
        //}

    }
}
